package com.springboot.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.aliyun.alidns20150109.Client;
import com.aliyun.alidns20150109.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.springboot.util.config.SystemProperties;
import com.springboot.util.http.MyHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
/**
 * @Classname DdnsClient
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/6/5 11:27
 * @Created by zyj
 */
@Component
public class DdnsClient {
    private final Logger logger= LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SystemProperties systemProperties;

    @Autowired
    private MyHttpUtil myHttpUtil;

    private String currentDdnsIp="";

    public Client client;

//    public DdnsClient() throws Exception {
//        this.client = new Client(new Config()
//                // 您的AccessKey ID
//                .setAccessKeyId(systemProperties.getAccessKeyId())
//                // 您的AccessKey Secret
//                .setAccessKeySecret(systemProperties.getAccessKeySecret())
//                .setEndpoint(systemProperties.getEndpoint())
//        );
//        this.refreshDdns();
//    }

    public void refreshDdns() throws Exception {
        this.client = new Client(new Config()
                // 您的AccessKey ID
                .setAccessKeyId(systemProperties.getAccessKeyId())
                // 您的AccessKey Secret
                .setAccessKeySecret(systemProperties.getAccessKeySecret())
                .setEndpoint(systemProperties.getEndpoint())
        );

        String currentIp = myHttpUtil.getNowIP();
        if (!this.currentDdnsIp.equals(currentIp)){
            // 获取解析记录
            List<DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord> domainDnsRecord = this.getDomainDnsRecord();
            // 刷新解析记录
            this.updateDomainDnsRecord(domainDnsRecord,currentIp);
        }
        logger.info("修改解析记录，当前IP：{}",currentIp);
    }


    private List<DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord> getDomainDnsRecord() throws Exception {
        DescribeDomainRecordsRequest describeDomainRecordsRequest = new DescribeDomainRecordsRequest()
                .setDomainName(systemProperties.getDomainName())
                .setType(systemProperties.getKeyType());
        // 获取记录详情
        DescribeDomainRecordsResponse domainRecordsResponse = client.describeDomainRecords(describeDomainRecordsRequest);
        List<DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord> record = domainRecordsResponse.getBody().getDomainRecords().getRecord();
        //DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord describeDomainRecordsResponseBodyDomainRecordsRecord = record.get(0);
        logger.info("获取解析记录：{}", JSON.toJSONString(record));
        return record;
    }
    private void updateDomainDnsRecord(List<DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord> domainDnsRecord,String newIp) throws Exception {

        for(DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord record: domainDnsRecord){

            logger.info("修改解析结果前 record：{}  newIp:{}", record.getValue(), newIp);
            if(!StrUtil.equals(record.getValue(), newIp)){
                // 修改记录
                UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest()
                        .setRecordId(record.getRecordId())
                        .setRR(record.getRR())
                        .setType(record.getType())
                        .setValue(newIp);
                // 修改记录
                UpdateDomainRecordResponse updateDomainRecordResponse = client.updateDomainRecord(updateDomainRecordRequest);
                logger.info("修改解析结果：{}",JSON.toJSONString(updateDomainRecordResponse));
            }

        }
    }
}


